<?php

namespace App\Http\Controllers\Volunteer;

use App\Enum\CodeEnum;
use App\Enum\OrderEnum;
use App\Http\Controllers\Api\ApiBaseController;
use App\Http\Controllers\Controller;
use App\Models\VillagerUserCommunityModel;
use App\Services\OrderService;
use App\Services\Tools\HelpService;
use App\Services\VolunteerService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends ApiBaseController
{

    protected $service = null;

    public function __construct()
    {
        $this->service = self::getServiceObj();
    }

    /**
     * 注册为志愿者
     * @param Request $request
     * @return JsonResponse|void
     * @author ninetyseven.
     * @date 2023-04-11 23:56
     */
    public function volunteerAdd(Request $request)
    {
        DB::beginTransaction();
        try {
            $post = $request->post();
            $res = VolunteerService::volunteerAdd($post);
            DB::commit();
            return success($res);
        } catch (\Exception $e) {
            DB::rollBack();
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 社区服务列表
     * serviceList
     * @param Request $request
     * @author ninetyseven.
     * @date 2023/4/30
     */
    public function serviceList(Request $request)
    {
        try {
            $data = VolunteerService::serviceList($request->get('user_id'));
            return success($data);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 保存志愿者服务社区
     * serviceSave
     * @param Request $request
     * @author ninetyseven.
     * @date 2023/4/30
     */
    public function serviceSave(Request $request)
    {
        try {
            $res = VolunteerService::serviceSave($request->post());
            if($res !== true){
                return fail(CodeEnum::ERROR, '保存失败');
            }

            return success();
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 已服务订单列表
     * @author ninetyseven.
     * @date 2023-04-15 16:56
     */
    public function getOrderList()
    {
        try {
            $data = VolunteerService::getOrderList(OrderEnum::STATUS_PENDING_TRAVEL);
            return success($data);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 待服务订单列表
     * @author ninetyseven.
     * @date 2023-04-15 16:56
     */
    public function getOrderWaitList()
    {
        try {
            $data = VolunteerService::getWaitOrderList();
            return success($data);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }
    /**
     * 志愿者：订单报名
     * @author ninetyseven.
     * @date 2023-04-15 16:56
     */
    public function orderApply()
    {
        try {
            $data = VolunteerService::orderApply(request()->post());
            return success('报名成功。');
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 志愿者活动报名
     * @return JsonResponse|null
     */
    public function activityApply()
    {
        try {
            $data = VolunteerService::activityApply(request()->post());
            return success('报名成功。');
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 志愿者：订单取消报名
     * @return JsonResponse|null
     */
    public function orderCancelApply()
    {
        try {
            $data = VolunteerService::orderCancelApply(request()->post());
            return success('取消订单报名成功。');
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }
    /**
     * 志愿者：活动取消报名
     * @return JsonResponse|null
     */
    public function activityCancelApply()
    {
        try {
            $data = VolunteerService::activityCancelApply(request()->post());
            return success('取消活动报名成功。');
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }
    public function getSecurityOrderList()
    {
        try {
            $data = VolunteerService::getSecurityOrderList();
            return success($data);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * // 活动列表-已报名
     * @return JsonResponse|null
     */
    public function getActivityList()
    {
        try {
            $data = VolunteerService::getActivityList();
            return success(HelpService::resultsPage($data));
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * // 活动列表-可报名
     * @return JsonResponse|null
     */
    public function getActivityWaitList()
    {
        try {
            $data = VolunteerService::getActivityList(false);
            return success(HelpService::resultsPage($data));
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 志愿者异常上报
     * abnormalAdd
     * @author ninetyseven.
     * @date 2023/7/6
     */
    public function abnormalAdd(): ?JsonResponse
    {
        try {
            $post = request()->post();
            $res = VolunteerService::abnormalAdd($post);
            if($res === false) {
                return fail(CodeEnum::ERROR, '上报失败');
            }

            return success();
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 服务中订单
     * getOrderInInfo
     * @author ninetyseven.
     * @date 2023/4/30
     */
    public function getOrderInInfo()
    {
        try {
            $data = VolunteerService::getOrderInfo(OrderEnum::ORDER_VOLUNTEER_YES);
            return success($data);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }

    /**
     * 志愿者信息
     * @author ninetyseven.
     * @date 2023-04-12 22:02
     */
    public function getInfo()
    {
        try {
            $info = $this->service->getVolunteerInfo();
            return success($info);
        } catch (\Exception $e) {
            return fail(CodeEnum::ERROR, $e->getMessage());
        }
    }


}
